const response=require('./response')
const jwt =require("jsonwebtoken") ;
const secret = 'tuisa';
const {noTokenPath}=require("./constData")

const sign = (data,usefulTime='24h') => {
    return jwt.sign(data, secret, { expiresIn: usefulTime });
};
const verify = async(ctx,next) => {
    // 从请求的 URL 中解析出 token 参数
    const url = new URL(ctx.url, `ws://${ctx.headers.host}`);
    let authHeader = url.searchParams.get('token');
    if (!authHeader) {
        console.log('token不存在')
       return false
    }
    try {
        let res = jwt.verify(authHeader, secret);
        if (res) {
            return res
        } else {

            return false
        }
    } catch (error) {

        return false
    }
};
module.exports = {
    sign,
    verify,
};
